<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
*/
class apply_db extends Model
{
	public $pk = 'apply_id';
	public $table = 'ptx_apply';
	var $linker = array(
			'user'=>array(
					'type' => 'hasone',
					'map' => 'user',
					'mapkey' => 'user_id',
					'ftable' => 'ptx_user',
					'fkey' => 'user_id',
					'enabled' => true
			),
	);
	var $select_fields = " ptx_apply.*,user.user_id,user.nickname,user.email ";

	//$type=1 mean star
	//$type=2 mean store
	public function search($type,$status,$page,$pagesize){
		if($type) $conditions['apply_type']=$type;
		if(is_numeric($status)) $conditions['status']=$status;
		return $this->pager($page, $pagesize)->findAllJoin($conditions," ptx_apply.apply_id DESC ",$this->select_fields);
	}

	public function deleteapply($conditions){
		$apply_id = $this->escape($conditions['apply_id']);
		if($apply_id){
			$this->delete($conditions);
		}
	}
	public function deletestarapply($uid){
		$this->delete(array('user_id'=>$uid,'apply_type'=>'1'));
	}
	public function find_one($conditions=NULL){
		return $this->findJoin($conditions,null,$this->select_fields);
	}

	public function getapplybyid($apid){
		return $this->findBy('apply_id', $apid);
	}

	public function checkstar_exits($user_id){
		$conditions['user_id'] = $user_id;
		$conditions['apply_type'] = '1';
		return $this->find($conditions,null," ptx_apply.apply_id ");
	}

	public function checkstore_exits($user_id){
		$conditions['user_id'] = $user_id;
		$conditions['apply_type'] = '2';
		return $this->find($conditions,null," ptx_apply.apply_id ");
	}

	public function add_starapply($user_id,$category_id,$message){
		$data['user_id'] = $user_id;
		$data['category_id'] = $category_id;
		$category = load_db('star/starcategory')->getCategoryByid($category_id);
		$data['message_txt'] = $message;
		$data['category_name'] = $category['category_name'];
		$data['apply_type'] = '1';
		$data['create_time'] = TIMESTAMP;
		return $this->create($data);
	}
	public function groupadmin_exits($user_id,$groupid){
		$conditions['user_id'] = $user_id;
		$conditions['category_id'] = $groupid;
		$conditions['apply_type'] = '3';
		return $this->find($conditions,null," ptx_apply.apply_id ");
	}
	
	public function add_groupadminapply($user_id,$groupid,$message){
		$data['user_id'] = $user_id;
		$data['category_id'] = $groupid;
		$group = load_db('group')->getGroupByid($groupid);
		$data['message_txt'] = $message;
		$data['category_name'] = $group['group_title'];
		$data['apply_type'] = '3';
		$data['create_time'] = TIMESTAMP;
		return $this->create($data);
	}
	public function add_storeapply($user_id,$category_id,$store_id,$message){
		$data['user_id'] = $user_id;
		$data['category_id'] = $category_id;
		$data['rel_id'] = $store_id;
		$category = load_db('store/storecategory')->getCategoryByid($category_id);
		$data['message_txt'] = $message;
		$data['category_name'] = $category['category_name'];
		$data['apply_type'] = '2';
		$data['create_time'] = TIMESTAMP;
		return $this->create($data);
	}

	public function change_category($condition,$categoryid){
		return $this->update($condition, array('category_id'=>$categoryid));
	}

	public function agree($apply_id){
		$conditions['apply_id'] = $apply_id;
		$apply = $this->find_one($conditions);
		if($apply){
			$this->update(array('apply_id'=>$apply_id),array('status'=>1));
			if($apply['apply_type']==1){
				load_db("user")->update_staruser($apply['user_id'],1);
				$ptx_staruser = load_db("star");
				$data['user_id'] = $apply['user_id'];
				$data['category_id'] = $apply['category_id'];
				$data['star_reason'] = $apply['message_txt'];
				$ptx_staruser->add_one($data);
				$ptx_staruser->update_staruser_cache();
			}elseif ($apply['apply_type']==2){
				load_db("user")->update_shopuser($apply['user_id'],1);
				load_db("store")->updateStoreByid($apply['rel_id'],array('user_id'=>$apply['user_id']));
			}elseif ($apply['apply_type']==3){
				$ugroupdb = load_db("group/ugroup");
				$groupid = $apply['category_id'];
				if($group = load_db("group")->find_one(array('group_id'=>$groupid))){
					$data['user_id'] = $apply['user_id'];
					$data['group_id'] = $groupid;
					if($ugroupdb->find($data)){
						$ugroupdb->update($data,array('is_admin'=>1));
					}else{
						$data['is_admin'] = 1;
						$ugroupdb->add_one($data);
					}
				}
			}
			//$this->deleteByPk($apply_id);
		}

		return;
	}

	public function disagree($apply_id){
		$conditions['apply_id'] = $apply_id;
		$apply = $this->find_one($conditions);
		if($apply){
			$this->update(array('apply_id'=>$apply_id),array('status'=>2));
		}
		return;
	}

	public function recusal($apply_id){
		$conditions['apply_id'] = $apply_id;
		$apply = $this->find_one($conditions);
		if($apply){
			$this->update(array('apply_id'=>$apply_id),array('status'=>3));
			if($apply['apply_type']==1){
				load_db("user")->update_staruser($apply['user_id'],0);
				$ptx_staruser = load_db("star");
				$data['user_id'] = $apply['user_id'];
				$data['category_id'] = $apply['category_id'];
				$ptx_staruser->delete($data);
				$ptx_staruser->update_staruser_cache();
			}elseif ($apply['apply_type']==2){
				load_db("user")->update_shopuser($apply['user_id'],0);
				load_db("store")->updateStoreByid($apply['rel_id'],array('user_id'=>'0'));
			}elseif ($apply['apply_type']==3){
				$ugroupdb = load_db("group/ugroup");
				$groupid = $apply['category_id'];
				if($group = load_db("group")->find_one(array('group_id'=>$groupid))){
					$data['user_id'] = $apply['user_id'];
					$data['group_id'] = $groupid;
					if($ugroupdb->find($data)){
						$ugroupdb->update($data,array('is_admin'=>0));
					}
				}
			}
		}

		return;
	}

}
